using UnityEngine;

namespace NodeCanvas.BehaviourTrees
{
	[AddComponentMenu("")]
	[Name("Debug")]
	[Category("Decorators")]
	[Description("Use this node to add a break point to the Behaviour Tree as well as log a UI label at the position of the agent when the node is in the selected status.")]
	[Icon("Log")]
	public class BTDebugger : BTDecorator
	{
		public bool breakPoint = true;

		public bool pauseUnity;

		public Status statusToLog = Status.Success;

		public string log;

		public bool forceReturnStatus;

		public Status forceStatus = Status.Success;

		private Component currentAgent;

		private Texture2D _tex;

		private Texture2D tex
		{
			get
			{
				if (!_tex)
				{
					_tex = new Texture2D(1, 1);
					_tex.SetPixel(0, 0, Color.white);
					_tex.Apply();
				}
				return _tex;
			}
		}

		protected override Status OnExecute(Component agent, Blackboard blackboard)
		{
			base.useGUILayout = !string.IsNullOrEmpty(log);
			currentAgent = agent;
			if (breakPoint)
			{
				base.graph.PauseGraph();
				Debug.Log("Behaviour Tree '" + base.graph.graphName + "' Breakpoint Reached...(Controls can be found on the BehaviourTreeOwner Inspector)");
				if (pauseUnity)
				{
					Debug.Break();
				}
			}
			if (base.decoratedConnection == null)
			{
				return (!forceReturnStatus) ? base.status : forceStatus;
			}
			base.status = base.decoratedConnection.Execute(agent, blackboard);
			return (!forceReturnStatus) ? base.status : forceStatus;
		}

		private void OnGUI()
		{
			if (!(currentAgent == null) && !(Camera.main == null) && base.status == statusToLog)
			{
				Vector2 center = Camera.main.WorldToScreenPoint(currentAgent.transform.position + new Vector3(0f, -0.5f, 0f));
				Vector2 vector = new GUIStyle("label").CalcSize(new GUIContent(log));
				Rect position = new Rect(0f, 0f, vector.x, vector.y);
				center.y = (float)Screen.height - center.y;
				position.center = center;
				GUI.color = new Color(1f, 1f, 1f, 0.5f);
				position.width += 6f;
				GUI.DrawTexture(position, tex);
				GUI.color = new Color(0.2f, 0.2f, 0.2f, 1f);
				position.x += 4f;
				GUI.Label(position, log);
				GUI.color = Color.white;
			}
		}
	}
}
